<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>源语|AtomGit</title>
    <style>
        body {


            font-family: 'Arial', sans-serif;
            background-color: #000000;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
            overflow: hidden;
        }

        h1 {
            text-align: center;
            color: #ffffff;
            font-size: 2.5em;
            letter-spacing: 1.05em;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
            margin: 20px 0;
            background: linear-gradient(to left,#4dabff,#0125ff);
        }

        #data-container {
            flex: 1;
            position: relative;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        #background-video {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            z-index: -1;
        }

        #items-wrapper {
            position: absolute;
            bottom: 0;
            right: 0;
            width: 100%;
            padding: 20px;
            box-sizing: border-box;
        }

        .item-container {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            min-height: 40px;
            margin-bottom: 10px;
            animation: slideUp 0.8s cubic-bezier(0.25, 0.1, 0.25, 1);
            border-radius: 20px;
            padding: 5px 15px;
            float: right;
            clear: both;
            background-color: rgba(255, 255, 255, 0.7);
            backdrop-filter: blur(5px);
            max-width: 66.67%;
            word-wrap: break-word;
            flex-wrap: wrap;
        }

        .item-container span {
            margin-right: 10px;
            max-width: calc(100% - 40px);
            white-space: pre-wrap;
            word-break: break-word;
        }

        .item-container img {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            object-fit: cover;
            flex-shrink: 0;
        }

        @keyframes slideUp {
            0% {
                transform: translateY(100%);
                opacity: 0;
            }

            100% {
                transform: translateY(0);
                opacity: 1;
            }
        }

        @keyframes fadeOutUp {
            to {
                opacity: 0;
                transform: translateY(-100%);
            }
        }

        .fade-out-up {
            animation: fadeOutUp 0.5s ease-out forwards;
        }

        footer {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: #ffffff;
            position: fixed;
            bottom: 10px;
            left: 0;
            right: 0;
        }

        footer,
        a {
            color: #ffffff;
        }
    </style>
</head>

<body>
    <h1>源语实时展示</h1>
    <div id="data-container">
        <video id="background-video" autoplay loop muted>
            <source src="https://file.pop.atomgit.com/videos/danmu/AtomGit_introduce_1080p.mp4" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        <div id="items-wrapper"></div>
    </div>
    <footer>数据来源：<a href="https://pagesorg.atomgit.net" target="_blank">Pages organization of AtomGit</a></footer>

    <script>
        const itemsWrapper = document.getElementById('items-wrapper');
        const maxItems = 30;

        function getRandomColor() {
            const hue = Math.floor(Math.random() * 360);
            return `hsla(${hue}, 70%, 80%, 0.7)`;
        }

        function getContrastColor(bgColor) {
            const color = bgColor.match(/\d+/g).map(Number);
            const yiq = (color[0] * 299 + color[1] * 587 + color[2] * 114) / 1000;
            return yiq >= 128 ? '#000' : '#fff';
        }

        function wrapText(text, maxWidth) {
            const words = text.split('');
            let lines = [];
            let currentLine = '';

            words.forEach(char => {
                const testLine = currentLine + char;
                const testWidth = getTextWidth(testLine);

                if (testWidth > maxWidth) {
                    lines.push(currentLine);
                    currentLine = char;
                } else {
                    currentLine = testLine;
                }
            });

            lines.push(currentLine);
            return lines.join('\n');
        }

        function getTextWidth(text) {
            const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
            const context = canvas.getContext("2d");
            context.font = "14px Arial"; // 确保这与您的CSS字体设置匹配
            return context.measureText(text).width;
        }

        const eventSource = new EventSource('https://wordcloud.atomgit.com/danmu/sse');

        eventSource.onmessage = (event) => {
            const dataStr = event.data;
            const data = JSON.parse(dataStr)

            const itemContainer = document.createElement('div');
            itemContainer.className = 'item-container';

            const bgColor = getRandomColor();
            const textColor = getContrastColor(bgColor);
            itemContainer.style.backgroundColor = bgColor;
            itemContainer.style.color = textColor;

            const newDataElement = document.createElement('span');
            const maxWidth = itemsWrapper.offsetWidth * 0.6667 - 70; // 2/3 of the container width minus padding and image width
            newDataElement.textContent = wrapText(data.word, maxWidth);
            itemContainer.appendChild(newDataElement);

            var img = new Image();
            img.src = data.pic;
            img.onload = () => {
                itemContainer.appendChild(img);
            };

            itemsWrapper.appendChild(itemContainer);

            if (itemsWrapper.children.length > maxItems) {
                const firstChild = itemsWrapper.firstChild;
                firstChild.classList.add('fade-out-up');
                setTimeout(() => {
                    itemsWrapper.removeChild(firstChild);
                }, 500);
            }

            // 调整其他项的位置
            const items = itemsWrapper.children;
            for (let i = 0; i < items.length - 1; i++) {
                items[i].style.transform = `translateY(-${(items.length - i - 1) * 10}px)`;
            }
        };

        eventSource.onerror = (error) => {
            console.error('Error occurred:', error);
        };

        function adjustContainerHeight() {
            const container = document.getElementById('data-container');
            const windowHeight = window.innerHeight;
            const headerHeight = document.querySelector('h1').offsetHeight;
            const footerHeight = document.querySelector('footer').offsetHeight;
            const containerHeight = windowHeight - headerHeight - footerHeight - 40; // 40px for margins
            container.style.height = `${containerHeight}px`;
        }

        window.addEventListener('load', adjustContainerHeight);
        window.addEventListener('resize', adjustContainerHeight);
    </script>
</body>

</html>